const app = getApp()
const db = wx.cloud.database()
import Dialog from '../../dist/dialog/dialog';
Page({
  data: {
    shoppingcart_list: {},
    checked: false,
    totalprice: 0,
    isCheck:false,
  },

  onShow: function(options) {
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo == "") {
      wx.navigateTo({
        url: '../auth/auth'
      })
    } else {
      db.collection('shoppingcart').where({
        _openid: userInfo._openid
      }).get().then(res => {
        this.setData({
          shoppingcart_list: res.data,
        })
        this.caculateTotalPrice()
        this.checkSelect()
      })
    }
  },

  caculateTotalPrice() {
    var totalprice = 0
    var shoppingcart_list = this.data.shoppingcart_list
    for (var i = 0; i < shoppingcart_list.length; i++) {
      if (shoppingcart_list[i].checked)
        totalprice = totalprice + shoppingcart_list[i].saleprice * shoppingcart_list[i].num
    }
    var isCheck=this.data.isCheck
    if(totalprice>0)
      isCheck=true
    else
      isCheck=false
    this.setData({
      totalprice: parseInt(totalprice * 100),isCheck:isCheck
    })
  },

  checkSelect() {
    var shoppingcart_list = this.data.shoppingcart_list
    var isAllSelect = true
    for (var i = 0; i < shoppingcart_list.length; i++) {
      if (!shoppingcart_list[i].checked && isAllSelect)
        isAllSelect = false
    }
    this.setData({
      checked: isAllSelect
    })
  },

  onSelectGood(event) {
    var index = event.currentTarget.dataset.index
    var id = event.currentTarget.dataset.id
    var shoppingcart_list = this.data.shoppingcart_list
    shoppingcart_list[index].checked = event.detail
    this.setData({
      shoppingcart_list: shoppingcart_list
    })
    this.caculateTotalPrice();
    this.checkSelect()
    db.collection('shoppingcart').doc(id).update({
      data: {
        checked: event.detail
      },
      success: function(res) {
        console.log(res)
      }
    })
  },

  onPlusNum: function(event) {
    var sum = app.globalData.goodnum + 1
    app.globalData.goodnum = sum
    wx.setTabBarBadge({
      index: 2,
      text: sum + ""
    })
  },

  onMinusNum: function(event) {
    var sum = app.globalData.goodnum - 1
    app.globalData.goodnum = sum
    wx.setTabBarBadge({
      index: 2,
      text: sum + ""
    })
  },

  onChangeNum(event) {
    var index = event.currentTarget.dataset.index
    var id = event.currentTarget.dataset.id
    var shoppingcart_list = this.data.shoppingcart_list
    if (event.detail == 0) {
      shoppingcart_list[index].num = 1
      this.setData({
        shoppingcart_list: shoppingcart_list
      })
      Dialog.confirm({
        message: '确认要删除宝贝吗？'
      }).then(() => {
        shoppingcart_list.splice(index, 1);
        this.setData({
          shoppingcart_list: shoppingcart_list
        })
        db.collection('shoppingcart').doc(id).remove({
          success: function(res) {
            wx.showToast({
              title: '删除成功...',
              icon: 'none',
              duration: 1500
            })
          },
          fail: console.error
        })
        Dialog.close();
      }).catch(() => {
        var sum = app.globalData.goodnum + 1
        app.globalData.goodnum = sum
        wx.setTabBarBadge({
          index: 2,
          text: sum + ""
        })
        Dialog.close();
      });
    } else {
      shoppingcart_list[index].num = event.detail
      if (!shoppingcart_list[index].checked) {
        db.collection('shoppingcart').doc(id).update({
          data: {
            checked: true
          },
          success: function(res) {}
        })
      }
      shoppingcart_list[index].checked = true
      this.setData({
        shoppingcart_list: shoppingcart_list
      })
      this.caculateTotalPrice();
      this.checkSelect()
      db.collection('shoppingcart').doc(id).update({
        data: {
          num: event.detail
        },
        success: function(res) {
          console.log(res)
        }
      })
    }
  },

  onSelectAll(event) {
    var shoppingcart_list = this.data.shoppingcart_list
    var checked = this.data.checked
    for (var i = 0; i < shoppingcart_list.length; i++) {
      shoppingcart_list[i].checked = !checked
      db.collection('shoppingcart').doc(shoppingcart_list[i]._id).update({
        data: {
          checked: !checked
        },
        success: function(res) {}
      })
    }
    this.setData({
      shoppingcart_list: shoppingcart_list,
      checked: !checked
    })
    this.caculateTotalPrice();
  },

  onClickButton() {
    var shoppingcart_list = this.data.shoppingcart_list
    var list = []
    for (var i = 0; i < shoppingcart_list.length; i++) {
      if (shoppingcart_list[i].checked)
        list.push(shoppingcart_list[i]);
    }
    wx.navigateTo({
      url: '../order/order?orderinfo=' + JSON.stringify(list)
    })
  }
})